<template>
  <el-dialog :title="isEdit ? '修改区域信息': '新增区域信息'" :visible.sync="visible" :close-on-click-modal="closeOnClickModal" @open="_handleDialogOpen" @close="_cancel">
    <el-form ref="dataForm" :model="form" :status-icon="true" :rules="formRules" label-width="100px">
      <el-form-item label="区域名称" prop="name">
        <el-input v-model.trim="form.name" :placeholder="`请输入区域名称`" maxlength="20" />
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button size="small" @click="_cancel">取 消</el-button>
      <el-button :loading="_loading" size="small" type="primary" @click="_ok">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { getById } from '@/api/systemManage/store'
import waves from '@/directive/waves' // Waves directive

export default {
  directives: { waves },
  components: {
    // DetailCropper
  },

  props: {
    closeOnClickModal: {
      type: Boolean,
      default: false
    },

    loading: {
      type: Boolean,
      default: false
    }
  },

  data() {
    return {
      selectHeadImgInput: null,
      visible: false,
      isEdit: false,
      form: {},
      formRules: {
        name: [{
          required: true,
          message: '请填写店铺名称'
        }]
      }
    }
  },

  computed: {
    '_loading': {
      get() {
        return this.loading
      },

      set(value) {
        this.$emit('update:loading', value)
      }
    }
  },
  methods: {

    _formReset() {
      this.form = {
        name: ''
      }
      this.$nextTick(() => {
        this.$refs.dataForm.resetFields()
      })
    },

    _handleDialogOpen() {
      this._formReset()
      this.$emit('open')
    },

    _cancel() {
      this._formReset()
      this.visible = false
      this.isLoading = false
      this.$emit('cancel')
    },

    _ok() {
      const form = this.$refs['dataForm']
      console.log(this.form)
      form.validate(valid => {
        if (valid) {
          const formData = JSON.parse(JSON.stringify(this.form))
          this.$emit('ok', formData, this.isEdit)
        }
      })
    },

    show(params) {
      this.visible = true
      if (params) {
        this.isEdit = true
        getById(params.id || params).then(resp => {
          const data = resp.data
          this.form = data
        }).catch(resp => {
          this.$message.error(resp.message)
        })
      } else {
        this.isEdit = false
      }
    },

    close() {
      this._cancel()
    }
  }
}
</script>
